package com.fescoservice.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.directwebremoting.annotations.Param;
import org.directwebremoting.annotations.RemoteProxy;
import org.directwebremoting.spring.SpringCreator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.fescoservice.config.PropertiesConfig;
import com.fescoservice.exception.FSException;
import com.fescoservice.model.Application;
import com.fescoservice.model.Company;
import com.fescoservice.model.Customer;
import com.fescoservice.service.CustomerService;
import com.fescoservice.util.Constants;
import com.fescoservice.util.MiscUtil;
import com.fescoservice.util.StringUtil;

@Controller
@RequestMapping("/action/Customer.htm")
@RemoteProxy(creator = SpringCreator.class, creatorParams = @Param(name = "beanName", value = "CustomerController"), name = "CustomerAjaxController")   

public class CustomerController {
	private static final Logger log = LogManager.getLogger(CustomerController.class);
	
	@Autowired
	private CustomerService customerService;
		


	@RequestMapping(params = "cmd=list")
	public String listCategory(String page,ModelMap map, HttpServletRequest request) throws FSException{
		//查询提条件
		String serchOptionSel = request.getParameter("SerchOptionSel")==null?"":(String)request.getParameter("SerchOptionSel");
		if(!MiscUtil.isNullOrEmpty(serchOptionSel)){			
			request.setAttribute("SerchOptionSel", serchOptionSel);
		}

		int pageNumber = Integer.parseInt((null==page || "".equals(page))?"1":page);
		String serchOptionValue  =  request.getParameter("SerchOptionValueSel")==null?"":(String)request.getParameter("SerchOptionValueSel");
		if(!MiscUtil.isNullOrEmpty(serchOptionValue)){			
			request.setAttribute("SerchOptionValueSel", serchOptionValue);
		}
		log.debug("~~~~~~~~Controller begin listCustomer~~~~~~~~");
		String language = request.getSession().getAttribute("userLanguage")==""?"Chinese":(String)request.getSession().getAttribute("userLanguage");
		//查询
		getListModel(map,serchOptionSel,serchOptionValue,language,pageNumber);
		return "manage/customerlist";
	}
	
	private ModelMap getListModel(ModelMap map, String serchOptionSel,String serchOptionValue,String language,int pageNumber) throws FSException{	
		/*
		if(null==language || "".equals(language.trim())){
			throw new FSException("FS_ERROR_SESSIONOUT","");
		}*/
		int first = PropertiesConfig.getInstance().getInt("ListSize.Customer")*(pageNumber-1);
		Long counts  = customerService.countCustomers(serchOptionSel,serchOptionValue);
		List list = customerService.getAllCustomers(serchOptionSel,serchOptionValue,first);
		map.addAttribute("customerlist", list);
		map.addAttribute("counts", counts);
		map.addAttribute("pageNumber", pageNumber);
		return map;
	}
	
	
	
	@RequestMapping(params = "cmd=addCustomerInit")
	public String addInit(ModelMap map,HttpServletRequest request) throws FSException{	
		String serchOptionSel = request.getParameter("SerchOptionSel")==null?"":(String)request.getParameter("SerchOptionSel");
		String serchOptionValue  =  (String)request.getParameter("SerchOptionValueSel");
		
		if(!MiscUtil.isNullOrEmpty(request.getParameter("SerchOptionSel"))){
			request.setAttribute("SerchOptionSel", serchOptionSel);
		}
		if(!MiscUtil.isNullOrEmpty(request.getParameter("SerchOptionValueSel"))){
			request.setAttribute("SerchOptionValueSel", serchOptionValue);
		}
		return "manage/addCustomer";
	}
	
	@RequestMapping(params = "cmd=addCustomer")
	public String addCustomer(Customer customer, ModelMap map,	HttpServletRequest request) throws FSException {
		String language = request.getSession().getAttribute("userLanguage")==""?"Chinese":(String)request.getSession().getAttribute("userLanguage");
		customer.setCreatorId((String)request.getSession().getAttribute("userId"));
		customer.setCreatorName((String)request.getSession().getAttribute("userName"));
		customer.setCreatorDept((String)request.getSession().getAttribute("userDept"));
		customer.setCreateDate(new Date());
		customerService.saveCustomer(customer);
		//查询提条件
		String serchOptionSel = request.getParameter("SerchOptionSel")==null?"":(String)request.getParameter("SerchOptionSel");
		if(!MiscUtil.isNullOrEmpty(serchOptionSel)){			
			request.setAttribute("SerchOptionSel", serchOptionSel);
		}
		
		String serchOptionValue  =  request.getParameter("SerchOptionValueSel")==null?"":(String)request.getParameter("SerchOptionValueSel");
		if(!MiscUtil.isNullOrEmpty(serchOptionValue)){			
			request.setAttribute("SerchOptionValueSel", serchOptionValue);
		}
		getListModel(map,serchOptionSel,serchOptionValue,language,1);
		return "manage/customerlist";
	}
	
	@RequestMapping(params = "cmd=viewCustomer")
	public String viewCustomerInit(ModelMap map,HttpServletRequest request) throws FSException{	
		System.out.println("==============================viewCustomer===================================L119================");
		String custId = (String)request.getParameter("custId");
		

		//查询提条件
		String serchOptionSel = request.getParameter("SerchOptionSel")==null?"":(String)request.getParameter("SerchOptionSel");
		if(!MiscUtil.isNullOrEmpty(serchOptionSel)){			
			request.setAttribute("SerchOptionSel", serchOptionSel);
		}
		
		String serchOptionValue  =  request.getParameter("SerchOptionValueSel")==null?"":(String)request.getParameter("SerchOptionValueSel");
		if(!MiscUtil.isNullOrEmpty(serchOptionValue)){			
			request.setAttribute("SerchOptionValueSel", serchOptionValue);
		}		
		
		Customer cust = customerService.findCustomerById(custId);
		map.addAttribute("Customer", cust);
		
		String userId = String.valueOf(request.getSession().getAttribute("userId"));
		String userRole = String.valueOf(request.getSession().getAttribute("userRole"));
		String userDept = String.valueOf(request.getSession().getAttribute("userDept"));
		
		
		Long counts  = customerService.countApplications(userId,userRole,userDept,custId);
		List list = customerService.getApplications(userId,userRole,userDept,custId);
		map.addAttribute("applicationslist", list);
		map.addAttribute("counts", counts);
		
		
		String flag = (String)request.getParameter("Flag");		
		
		if(Constants.FS_T_VIEW_CUSTOMER_FLAG_LOGIN.equals(flag)){
			return "customer/custhome";
		}else if(Constants.FS_T_VIEW_CUSTOMER_FLAG_HR_LOGIN.equals(flag)){
			return "hr/viewHRLoginCustomer";
		}else{
			return "manage/viewCustomer";
		}

	}
	
	
	@RequestMapping(params = "cmd=modifyCustomerInit")
	public String modifyInit(ModelMap map,HttpServletRequest request) throws FSException{	
		System.out.println("==============================modifyCustomerInit===================================L79-================");
		String custId = (String)request.getParameter("custId");
		
		//查询提条件
		String serchOptionSel = request.getParameter("SerchOptionSel")==null?"":(String)request.getParameter("SerchOptionSel");
		if(!MiscUtil.isNullOrEmpty(serchOptionSel)){			
			request.setAttribute("SerchOptionSel", serchOptionSel);
		}
		
		String serchOptionValue  =  request.getParameter("SerchOptionValueSel")==null?"":(String)request.getParameter("SerchOptionValueSel");
		if(!MiscUtil.isNullOrEmpty(serchOptionValue)){			
			request.setAttribute("SerchOptionValueSel", serchOptionValue);
		}
		
		
		Customer cust = customerService.findCustomerById(custId);
		request.setAttribute("Customer", cust);
		
		return "manage/modifyCustomer";
	}
	
	
	@RequestMapping(params = "cmd=modifyCustomer")
	public String modifyCustomer(Customer customer, ModelMap map,	HttpServletRequest request) throws FSException {
		String language = (String)request.getSession().getAttribute("userLanguage");
		System.out.println("L184---------------customer.getCompanyId():"+customer.getCompanyId());
		customerService.updateCustomer(customer);
		
		String serchOptionSel = request.getParameter("SerchOptionSel")==null?"":(String)request.getParameter("SerchOptionSel");
		if(!MiscUtil.isNullOrEmpty(serchOptionSel)){			
			request.setAttribute("SerchOptionSel", serchOptionSel);
		}
		
		String serchOptionValue  =  request.getParameter("SerchOptionValueSel")==null?"":(String)request.getParameter("SerchOptionValueSel");
		if(!MiscUtil.isNullOrEmpty(serchOptionValue)){			
			request.setAttribute("SerchOptionValueSel", serchOptionValue);
		}
		getListModel(map,serchOptionSel,serchOptionValue,language,1);
		

		return "manage/customerlist";
	}
	

	
	
	
	@RequestMapping(params = "cmd=deleteCustomer")
	public String deleteCustomer(Customer customer, ModelMap map,	HttpServletRequest request) throws FSException {
		String language = (String)request.getSession().getAttribute("userLanguage");

		String serchOptionSel = request.getParameter("SerchOptionSel")==null?"":(String)request.getParameter("SerchOptionSel");
		if(!MiscUtil.isNullOrEmpty(serchOptionSel)){			
			request.setAttribute("SerchOptionSel", serchOptionSel);
		}
		
		String serchOptionValue  =  request.getParameter("SerchOptionValueSel")==null?"":(String)request.getParameter("SerchOptionValueSel");
		if(!MiscUtil.isNullOrEmpty(serchOptionValue)){			
			request.setAttribute("SerchOptionValueSel", serchOptionValue);
		}
		
		customerService.deleteCustomer(customer);
		getListModel(map,serchOptionSel,serchOptionValue,language,1);
		return "manage/customerlist";
	}
	
	@RequestMapping(params = "cmd=addApplicationInit")
	public String addApplicationInit(ModelMap map,HttpServletRequest request) throws FSException{	
		String serchOptionSel = request.getParameter("SerchOptionSel")==null?"":(String)request.getParameter("SerchOptionSel");
		String serchOptionValue  =  (String)request.getParameter("SerchOptionValueSel");
		
		if(!MiscUtil.isNullOrEmpty(request.getParameter("SerchOptionSel"))){
			request.setAttribute("SerchOptionSel", serchOptionSel);
		}
		if(!MiscUtil.isNullOrEmpty(request.getParameter("SerchOptionValueSel"))){
			request.setAttribute("SerchOptionValueSel", serchOptionValue);
		}
		
		request.setAttribute("creatorId", request.getSession().getAttribute("userId"));
		request.setAttribute("creatorName", request.getSession().getAttribute("userName"));
		request.setAttribute("creatorDept", request.getSession().getAttribute("userDept"));
		request.setAttribute("custId", request.getParameter("custId"));
		
		
		request.setAttribute("companyId", request.getSession().getAttribute("companyId"));
		request.setAttribute("companyName", request.getSession().getAttribute("companyName"));
		request.setAttribute("companyAddr", request.getSession().getAttribute("companyAddr"));
		
		
		
		return "manage/addApplication";
	}
	
	
	
	@RequestMapping(params = "cmd=addApplication")
	public String addApplication(Application application, ModelMap map,	HttpServletRequest request) throws FSException {
		String language = request.getSession().getAttribute("userLanguage")==""?"Chinese":(String)request.getSession().getAttribute("userLanguage");
		application.setCreatorId(String.valueOf(request.getSession().getAttribute("userId")));
		application.setCreatorName(String.valueOf(request.getSession().getAttribute("userName")));
		application.setCreatorDept(String.valueOf(request.getSession().getAttribute("userDept")));
		application.setCreateDate(new Date());
		
		
		String payDate = application.getPayDate();
		Date now = new Date();
		SimpleDateFormat dateformat=new SimpleDateFormat("MM/dd/yyyy");
		String nowDate=dateformat.format(new Date());
		
		if(MiscUtil.compareDate(nowDate,payDate)){
			application.setFeeStatus(Constants.FS_T_APPLICATION_FEE_STATUS_FINISH)	;
		}else{
			application.setFeeStatus(Constants.FS_T_APPLICATION_FEE_STATUS_NOT)	;
		}
		customerService.saveApplication(application);
		
		return viewCustomerInit(map,request);
	}
	
	
	@RequestMapping(params = "cmd=modifyApplicationInit")
	public String modifyApplicationInit(ModelMap map,HttpServletRequest request) throws FSException{	
		String serchOptionSel = request.getParameter("SerchOptionSel")==null?"":(String)request.getParameter("SerchOptionSel");
		String serchOptionValue  =  (String)request.getParameter("SerchOptionValueSel");
		
		if(!MiscUtil.isNullOrEmpty(request.getParameter("SerchOptionSel"))){
			request.setAttribute("SerchOptionSel", serchOptionSel);
		}
		if(!MiscUtil.isNullOrEmpty(request.getParameter("SerchOptionValueSel"))){
			request.setAttribute("SerchOptionValueSel", serchOptionValue);
		}
		
		String custId = (String)request.getParameter("custId");
		String applyId = (String)request.getParameter("applyId");
		
		
		request.setAttribute("creatorId", request.getSession().getAttribute("userId"));
		request.setAttribute("creatorName", request.getSession().getAttribute("userName"));
		request.setAttribute("creatorDept", request.getSession().getAttribute("userDept"));
		request.setAttribute("custId", request.getParameter("custId"));
		
		
		request.setAttribute("companyId", request.getSession().getAttribute("companyId"));
		request.setAttribute("companyName", request.getSession().getAttribute("companyName"));
		request.setAttribute("companyAddr", request.getSession().getAttribute("companyAddr"));
		
		List list = customerService.viewApplications(applyId,custId);
		map.addAttribute("application", list.get(0));
		
		return "manage/modifyApplication";
	}
	
	
	
	@RequestMapping(params = "cmd=viewApplication")
	public String viewApplication(Application application, ModelMap map,	HttpServletRequest request) throws FSException {
		String language = request.getSession().getAttribute("userLanguage")==""?"Chinese":(String)request.getSession().getAttribute("userLanguage");

		System.out.println("==============================viewCustomer===================================L119================");
		String custId = (String)request.getParameter("custId");
		String applyId = (String)request.getParameter("applyId");

		
		//查询提条件
		String serchOptionSel = request.getParameter("SerchOptionSel")==null?"":(String)request.getParameter("SerchOptionSel");
		if(!MiscUtil.isNullOrEmpty(serchOptionSel)){			
			request.setAttribute("SerchOptionSel", serchOptionSel);
		}
		
		String serchOptionValue  =  request.getParameter("SerchOptionValueSel")==null?"":(String)request.getParameter("SerchOptionValueSel");
		if(!MiscUtil.isNullOrEmpty(serchOptionValue)){			
			request.setAttribute("SerchOptionValueSel", serchOptionValue);
		}		

		List list = customerService.viewApplications(applyId,custId);
		map.addAttribute("application", list.get(0));
		
		
		String flag = (String)request.getParameter("Flag");

		
		
		if(Constants.FS_T_VIEW_CUSTOMER_FLAG_LOGIN.equals(flag)){
			return "customer/viewCustApplication";	
		}else if(Constants.FS_T_VIEW_CUSTOMER_FLAG_HR_LOGIN.equals(flag)){
			return "hr/viewHRApplication";	
		}else{
			return "manage/viewApplication";
		}
	}
	
	
	
	@RequestMapping(params = "cmd=modifyApplication")
	public String modifyApplication(Application application,ModelMap map,HttpServletRequest request) throws FSException{	
		String serchOptionSel = request.getParameter("SerchOptionSel")==null?"":(String)request.getParameter("SerchOptionSel");
		String serchOptionValue  =  (String)request.getParameter("SerchOptionValueSel");
		
		if(!MiscUtil.isNullOrEmpty(request.getParameter("SerchOptionSel"))){
			request.setAttribute("SerchOptionSel", serchOptionSel);
		}
		if(!MiscUtil.isNullOrEmpty(request.getParameter("SerchOptionValueSel"))){
			request.setAttribute("SerchOptionValueSel", serchOptionValue);
		}
		
		request.setAttribute("creatorId", request.getSession().getAttribute("userId"));
		request.setAttribute("creatorName", request.getSession().getAttribute("userName"));
		request.setAttribute("creatorDept", request.getSession().getAttribute("userDept"));
		request.setAttribute("custId", request.getParameter("custId"));
		
		
		request.setAttribute("companyId", request.getSession().getAttribute("companyId"));
		request.setAttribute("companyName", request.getSession().getAttribute("companyName"));
		request.setAttribute("companyAddr", request.getSession().getAttribute("companyAddr"));
		
		String payDate = application.getPayDate();
		Date now = new Date();
		SimpleDateFormat dateformat=new SimpleDateFormat("MM/dd/yyyy");
		String nowDate=dateformat.format(new Date());
		
		if(MiscUtil.compareDate(nowDate,payDate)){
			application.setFeeStatus(Constants.FS_T_APPLICATION_FEE_STATUS_FINISH)	;
		}else{
			application.setFeeStatus(Constants.FS_T_APPLICATION_FEE_STATUS_NOT)	;
		}
		
		
		customerService.updateApplication(application);
		return viewCustomerInit(map,request);
	}
	
	
	
	@RequestMapping(params = "cmd=deleteApplication")
	public String deleteApplication(Application application, ModelMap map,	HttpServletRequest request) throws FSException {
		String language = request.getSession().getAttribute("userLanguage")==""?"Chinese":(String)request.getSession().getAttribute("userLanguage");
		application.setCreatorId(String.valueOf(request.getSession().getAttribute("userId")));
		application.setCreatorName(String.valueOf(request.getSession().getAttribute("userName")));
		application.setCreatorDept(String.valueOf(request.getSession().getAttribute("userDept")));
		application.setCreateDate(new Date());
		customerService.deleteApplication(application);
		
		return viewCustomerInit(map,request);
	}
	
	
	
	
}
